package com.mbc.hr.manager;

import java.util.List;

import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.hr.entities.JobTitle;

public class JobTitleMgr extends AbstractMgr {
	
	public JobTitleMgr(){}
	
	public JobTitleMgr(String sessionId){
		this.sessionId = sessionId;
	}
	
	public List<JobTitle> getAll()throws Exception{
		openConnection();
		try {
			return entityManager.createNamedQuery("JobTitle.findAll",JobTitle.class).getResultList();
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public JobTitle find(long jobtitleid)throws Exception{
		openConnection();
		try {
			return entityManager.find(JobTitle.class, jobtitleid);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void create(JobTitle jobtitle)throws Exception{
		openConnection();
		try {
			beginTransaction();
			jobtitle.setJobTitleID(KeyGenerator.generateKey(entityManager));
			entityManager.persist(jobtitle);
			commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void update(JobTitle jobtitle)throws Exception{
		openConnection();
		try {
			beginTransaction();
			JobTitle l_jobtitle = entityManager.find(JobTitle.class,jobtitle.getJobTitleID());
			l_jobtitle.setJobTitleID(jobtitle.getJobTitleID());
			l_jobtitle.setTitle(jobtitle.getTitle());
			entityManager.merge(l_jobtitle);
			commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void delete(long jobtitleid)throws Exception{
		openConnection();
		try {
			beginTransaction();
			JobTitle l_jobtitle = entityManager.find(JobTitle.class, jobtitleid);
			entityManager.remove(l_jobtitle);
			commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
}
